package com.gryffonx.NameGenerators;

import com.gryffonx.DiceRoller.StaticDiceRoller;
import com.gryffonx.utils.StringUtils;
import com.gryffonx.utils.XMLUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

import java.io.File;

/**
 * @author Erik Stell
 * @version 1.0
 */
public class VillageNameGenerator {
    private static final File fileToBeParsed = new File("src/com/gryffonx/xml/villages.xml");
    private static final SAXReader reader = new SAXReader();
    private static Document xmlDoc = null;

    public static String generateName(int howMany) {
        StringBuffer retVal = new StringBuffer();

        if (howMany <= 0) {
            howMany = 1;
        }

        try {
            xmlDoc = reader.read(fileToBeParsed);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        //determine the pattern that will be used.
        int whatPattern = determinePattern();

        for (int i = 0; i < howMany; i++) {
            StringBuffer adjVal = new StringBuffer();

            if (whatPattern == 1) {
                adjVal.append(generateGroup1());
            } else if (whatPattern == 2) {
                adjVal.append(generateGroup2());
            } else if (whatPattern == 3) {
                adjVal.append(generateGroup3());
            } else if (whatPattern == 4) {
                adjVal.append(generateGroup4());
            } else if (whatPattern == 5) {
                adjVal.append(generateGroup5());
            }
            //assign the retVal to the name generated
            retVal.append(adjVal);
            //retVal.append("\n");
        }
        return retVal.toString();
    }

    /**
     * PREFIX 1 + SUFFIX2
     * i.e., Whitefield
     *
     * @return
     */
    private static String generateGroup1() {
        StringBuffer retVal = new StringBuffer();
        try {
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 1)));
            retVal.append(XMLUtils.getNodeValue("suffix", xmlDoc, 2));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retVal.toString();
    }

    /**
     * PREFIX 1 + SUFFIX 1
     * i.e., Red Hollow
     *
     * @return
     */
    private static String generateGroup2() {
        StringBuffer retVal = new StringBuffer();
        try {
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 1)));
            retVal.append(" ");
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("suffix", xmlDoc, 1)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retVal.toString();
    }

    /**
     * NAME + SUFFIX1
     * i.e., Emond's Field
     *
     * @return
     */
    private static String generateGroup3() {
        StringBuffer retVal = new StringBuffer();
        try {
            //TODO:  refactor to use a surname as opposed to first name
            retVal.append(HumanNameGenerator.generateName());
            retVal.append("'s ");
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("suffix", xmlDoc, 1)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retVal.toString();
    }

    /**
     * SUFFIX1 + SUFFIX1
     * i.e., Glenmire
     *
     * @return
     */
    private static String generateGroup4() {
        StringBuffer retVal = new StringBuffer();
        try {
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("suffix", xmlDoc, 1)));
            retVal.append(XMLUtils.getNodeValue("suffix", xmlDoc, 1));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retVal.toString();
    }

    /**
     * SUFFIX1 + SUFFIX2
     * i.e., Glenmire
     *
     * @return
     */
    private static String generateGroup5() {
        StringBuffer retVal = new StringBuffer();
        try {
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("suffix", xmlDoc, 1)));
            retVal.append(XMLUtils.getNodeValue("suffix", xmlDoc, 2));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retVal.toString();
    }

    private static int determinePattern() {
        return StaticDiceRoller.rolldSomething(5, 1);
    }
}
